package org.xmlpersist.parser;

import java.io.File;
import java.io.IOException;
import java.util.*;

import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

/***
 * XMLParser parses xml and segregate parent and child tags
 * 
 * @author rjinoy
 * 
 */
public class XMLParser {
	private Document document = null;

	private List<Map<String, Map<String, String>>> masterList = new ArrayList<Map<String, Map<String, String>>>();

	private Map<String, Map<String, String>> eachRecordMap = null;

	private Map<String, String> columnDataMap = null;

	private Map<String, Set<String>> tags = new HashMap<String, Set<String>>();

	private List<String> tableList;

	private Set<String> childTags;

	public XMLParser() {
	}

	public void parseXMLFile(String xmlFile) {

		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
				.newInstance();

		try {
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			document = docBuilder.parse(new File(xmlFile));
		} catch (ParserConfigurationException e) {

			e.printStackTrace();
		} catch (SAXException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}

		Element element = document.getDocumentElement();
		NodeList tablesList = element.getChildNodes();
		if (tablesList.getLength() > 0) {
			masterList = new ArrayList<Map<String, Map<String, String>>>();
			for (int i = 0; i < tablesList.getLength(); i++) {
				eachRecordMap = new HashMap<String, Map<String, String>>();
				Node table = tablesList.item(i);
				if (table.getNodeType() == Node.ELEMENT_NODE) {
					if (tags.get(table.getNodeName()) == null) {
						childTags = new TreeSet<String>();
						tags.put(table.getNodeName(), childTags);
					}
					NodeList columnList = table.getChildNodes();
					columnDataMap = new HashMap<String, String>();
					for (int j = 0; j < columnList.getLength(); j++) {
						Node column = columnList.item(j);
						if (column.getNodeType() == Node.ELEMENT_NODE) {
							columnDataMap.put(column.getNodeName(), column
									.getTextContent());
							((TreeSet) tags.get(table.getNodeName()))
									.add(column.getNodeName());
						}

					}
					eachRecordMap.put(table.getNodeName(), columnDataMap);
					masterList.add(eachRecordMap);
				}
			}
		}
		tableList = new ArrayList(tags.keySet());
	}

	/***
	 * List of all the parent and child tags
	 * 
	 * @return List
	 */
	public List<Map<String, Map<String, String>>> getMasterList() {
		return this.masterList;
	}

	public Map<String, Map<String, String>> getEachRecordMap() {
		return eachRecordMap;
	}

	public Map<String, String> getColumnDataMap() {
		return columnDataMap;
	}

	public Set<String> getChildTags() {
		return childTags;
	}

	/***
	 * Returns Map of unique tags for creation of table
	 * 
	 * @return Map
	 */
	public Map<String, Set<String>> getTags() {
		return tags;
	}

	public List<String> getTableList() {
		return tableList;
	}

}